package login;

import javax.persistence.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import java.io.PrintWriter;

import java.io.BufferedReader;
import java.io.FileReader;
import bean.Message;

@WebServlet(name = "Login_C",urlPatterns = {"/Login_C"})
public class Login_C extends HttpServlet {
    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    protected void login_check(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException
    {
        resp.setContentType("text/html;charset=UTF-8");
        try {
            boolean flag = false;
            // 使用字符缓冲输入流读取user.txt文件中的内容
            BufferedReader br = null;
            br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\IdeaProjects\\project1\\src\\main\\user.txt"));
            // 一次读取一行
            String line = null;
            PrintWriter pw=resp.getWriter();
            while ((line = br.readLine()) != null) {
                // 字符串的分割功能
                String[] datas = line.split(" ");
                // 判断
                if (datas[0].equals(req.getParameter("username")))
                {   if(datas[2].equals(req.getParameter("password")))
                {
                    Message message=new Message("", "", "", "", "cat.jpg", null);
                    req.getSession().setAttribute("LocalUser",message);
                    flag = true;
                    break;
                }
                else
                {
                    pw.println("<center><button onclick=\"window.location.href='./login.jsp'\">重新登录</button></center>");
                    pw.println("<script language='javascript'>alert('密码错误,请重新输入')</script>");
                    break;
                }
                }
            }
            if(flag==true)
            {

                resp.sendRedirect("/show.jsp");

            }
            if(line==null)
            {
                pw.println("<center><button onclick=\"window.location.href='./login.jsp'\">重新登录</button></center>");
                pw.println("<center><button onclick=\"window.location.href='./register.jsp'\">前往注册</button></center>");
                pw.println("<script language='javascript'>alert('用户不存在,请重新登录或者前往注册')</script>");
            }
        }
        finally {

        }



    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        login_check(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
